package tcpudp

import "github.com/project-flogo/core/data/coerce"

import "fmt"

type Packet struct {
	Framer  string
	Payload []byte
}

func (p *Packet) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"framer":  p.Framer,
		"payload": p.Payload,
	}
}

func NewPacketFromMap(data map[string]interface{}) (Packet, error) {
	framer, err := coerce.ToString(data["framer"])
	if err != nil {
		return Packet{}, fmt.Errorf("failed create Packet, missing field framer")
	}
	payload, err := coerce.ToBytes(data["payload"])
	if err != nil {
		return Packet{}, fmt.Errorf("failed create Packet, missing payload")
	}
	p := Packet{
		Framer:  framer,
		Payload: payload,
	}
	return p, nil
}
